package zhou;

import java.io.*;
import java.util.Scanner;

/**
 * Created by Administrator on 2015/7/5.
 */
public class ConvertFile {
    private static int byteNum = 5;//要交换的字节数
    private static String fileDir = "C:\\Users\\Administrator\\Desktop\\视频";
    private static String file1Dir = "G:\\abc\\abc2\\abc1\\abc2\\abc1\\abc2";
    private static String file2Dir = "H:\\abc\\abc2\\abc1\\abc2\\abc1\\abc2";

    public static void main(String args[]){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入密码：");
        if("zhouiloveyou".equals(scanner.nextLine())){
            System.out.println("请输入转换哪个文件（1,桌面   2,G盘   3,H盘 其他(可以自由支配)    ）");
            String choose = scanner.nextLine();
            if(choose==null && "".equals(choose)){
                System.out.println("选项输入有误");
                return ;
            }
            if(choose.contains("1")){
                convertDirFile(fileDir);
            }
            if(choose.contains("2")){
                convertDirFile(file1Dir);
            }
            if(choose.contains("3")){
                convertDirFile(file2Dir);
            }
        }else{
            System.out.println("密码输入错误！");
        }
    }

    private static void convertDirFile(String fileDir){
        File file = new File(fileDir);
        File fileList[] = file.listFiles();
        int convertFileNum = 0;
        for(int i=0;i<fileList.length;i++){
            System.out.println("开始转换"+fileList[i].getName());
            try {
                if(file.length()>2){
                    convertByte(fileList[i]);
                    convertFileNum++;
                }else{
                    System.out.println("这个文件太小不能转换 fileName="+file.getName());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("共转换 " +fileList.length+ " 个文件，成功转换 "+convertFileNum+" 个!");
        System.out.println("");
        System.out.println("");
        System.out.println("");
    }

    //交换字节类
    private static void convertByte(File file) throws IOException {
        System.out.println("开始转换：time="+System.currentTimeMillis());
        RandomAccessFile randomAccessFile = new RandomAccessFile(file,"rw");
        randomAccessFile.seek(0);
        byte b[] = new byte[byteNum];
        randomAccessFile.read(b);
        randomAccessFile.seek(0);
        byte b1[] = new byte[byteNum];
        for(int i=0;i<byteNum;i++){
            b1[i]=b[byteNum-i-1];
        }
        randomAccessFile.write(b1);
        randomAccessFile.close();
        System.out.println("转换成功：time="+System.currentTimeMillis());
    }
}
